package com.java.controller;

import com.java.bean.Message;
import com.java.mvc.ResponseBody;
import com.java.service.AdminService;
import com.java.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

public class AdminController {
    @ResponseBody("/admin/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        boolean result = AdminService.login(username, password);
        //根据结果返回不同数据给ajax
        Message msg =null;
        if(result){
            msg = new Message(0,"登陆成功");
            //登陆时间 和 ip 的更新
            Date date = new Date();
            String ip = request.getRemoteAddr();
            AdminService.updateLoginTimeAndIP(username,date,ip);
            request.getSession().setAttribute("adminUserName","username");
        }else {
            msg = new Message(-1,"登陆失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
